//
//  NSDate+MLKit.m
//  MSHandbook
//
//  Created by molon on 15/3/27.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "NSDate+MLKit.h"

@implementation NSDate (MLKit)

- (NSDate*)dateByAddYearCount:(NSUInteger)count
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setYear:count];
    return [calendar dateByAddingComponents:dateComponents toDate:self  options:0];
}

- (NSDate*)dateByAddDayCount:(NSUInteger)count
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setDay:count];
    return [calendar dateByAddingComponents:dateComponents toDate:self  options:0];
}

- (NSString*)normalFormatString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:self];
}


static NSArray * kChineseWeekdays() {
    static NSArray *_chineseWeekdays = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _chineseWeekdays = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    });
    
    return _chineseWeekdays;
}


- (NSString*)chineseWeekday
{
    NSDateComponents *componets = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:self];
    NSInteger weekday = [componets weekday];
    
    return kChineseWeekdays()[weekday-1];
}


- (NSInteger)year
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar components:NSCalendarUnitYear fromDate:self].year;
}

- (NSInteger)month
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar components:NSCalendarUnitMonth fromDate:self].month;
}

- (NSInteger)day
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    return [calendar components:NSCalendarUnitDay fromDate:self].day;
}

+ (NSInteger)yearsBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar rangeOfUnit:NSYearCalendarUnit startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSYearCalendarUnit startDate:&toDate
                 interval:NULL forDate:toDateTime];
    
    NSDateComponents *difference = [calendar components:NSYearCalendarUnit
                                               fromDate:fromDate toDate:toDate options:0];
    
    return ABS([difference year]);
}


+ (NSInteger)daysBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate
                 interval:NULL forDate:toDateTime];
    
    NSDateComponents *difference = [calendar components:NSDayCalendarUnit
                                               fromDate:fromDate toDate:toDate options:0];
    
    return ABS([difference day]);
}

+ (NSInteger)minutesBetweenDate:(NSDate*)fromDateTime andDate:(NSDate*)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar rangeOfUnit:NSMinuteCalendarUnit startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSMinuteCalendarUnit startDate:&toDate
                 interval:NULL forDate:toDateTime];
    
    NSDateComponents *difference = [calendar components:NSMinuteCalendarUnit
                                               fromDate:fromDate toDate:toDate options:0];
    
    return ABS([difference minute]);
}

+ (NSString*)todayString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSString*)nowString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSDate*)dateWithDayNormalString:(NSString*)dateString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter dateFromString:dateString];
}

+ (NSDate*)dateWithSecondNormalString:(NSString*)dateString
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter dateFromString:dateString];
}

- (NSString*)commonChineseStringWithStyle:(TimeChineseStringStyle)style
{
    //小于一天的就返回
    NSInteger betweenDayCount = [NSDate daysBetweenDate:[NSDate date] andDate:self];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSString *formatter = @"yyyy-MM-dd";
    if (betweenDayCount<=0) {
        formatter = @"H:mm";
    }else if (betweenDayCount==1){
        formatter = @"昨天";
    }else if(betweenDayCount<7){
        formatter = [self chineseWeekday];
    }else{
        if (style == TimeChineseStringStyleVerySimple) {
            formatter = @"yy-M-d";
        }else if (style == TimeChineseStringStyleSimple) {
            //如果是当年则不显示年，否则显示年月日
            NSInteger betweenYearCount = [NSDate yearsBetweenDate:[NSDate date] andDate:self];
            if (betweenYearCount==0) {
                formatter = @"M月d日";
            }else{
                formatter = @"yyyy年M月d日";
            }
        }
    }
    if (style == TimeChineseStringStyleSimple&&betweenDayCount!=0){
        formatter = [formatter stringByAppendingString:@" H:mm"];
    }
    
    [dateFormatter setDateFormat:formatter];
    
    return [dateFormatter stringFromDate:self];
}


#pragma mark - unix timestamp
+ (NSString*)unixMSTimestampWithNormalDayString:(NSString*)normalDayString
{
    if (normalDayString.length<=0) {
        return nil;
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    double timestamp = [[dateFormatter dateFromString:normalDayString]timeIntervalSince1970]*1000;
    return [NSString stringWithFormat:@"%.0f",timestamp];
}

+ (NSString*)normalFormatStringWithUnixMSTimestamp:(NSString*)unixMSTimestamp
{
    if (unixMSTimestamp.length<=0) {
        return nil;
    }
    return [[NSDate dateWithTimeIntervalSince1970:([unixMSTimestamp doubleValue]/1000)]normalFormatString];
}

+ (NSString*)normalFormatStringWithUnixMSTimestamp:(NSString*)unixMSTimestamp formatString:(NSString*)formatString
{
    if (unixMSTimestamp.length<=0) {
        return nil;
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatString];
    return [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:([unixMSTimestamp doubleValue]/1000)]];
}

+ (NSDate*)dateWithUnixMSTimestamp:(NSString*)unixMSTimestamp
{
    return [NSDate dateWithTimeIntervalSince1970:([unixMSTimestamp doubleValue]/1000)];
}

@end
